// pages/withdraw/withdraw.js
const app = getApp()
const Host = require('../../config').Host
var WxParse = require('../../wxParse/wxParse.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        allWithdraw: 0.00,
        value: 0.00,
        name: '',
        bank: '',
        bankNumber: '',
        phoneNumber: '',
      array: ['工商银行', '农业银行', '中国银行', '建设银行', '招商银行', '邮储银行', '交通银行', '浦发银行', '民生银行', '兴业银行', '平安银行', '中信银行', '华夏银行', '广发银行', '光大银行', '北京银行', '宁波银行'],
        checked: false,
        mengShow: false,
        index: 0,
        contract_content: '',
        salary:0.00,
        jifen:0.00,
        radio1:'bankcard',
        drawmethod:'salary',
    },
    input1: function(e) {
        var that = this;
        this.setData({
            value: e.detail.value,
        })
    },
    input2: function(e) {
        var that = this;
        this.setData({
            name: e.detail.value,
        })
    },
    input3: function(e) {
        var that = this;
        var value = that.data.array[e.detail.value];
        this.setData({
            bank: value,
        })
    },
    input4: function(e) {
        var that = this;
        this.setData({
            bankNumber: e.detail.value,
        })
    },
    input5: function(e) {
        var that = this;
        this.setData({
            phoneNumber: e.detail.value,
        })
    },
    withdrawMoney: function() {
        var that = this;
        if (!that.data.value) {
            wx.showToast({
                title: '请输入金额',
                icon: 'none',
                duration: 1500
            });
        } else if (parseFloat(that.data.value) > that.data.allWithdraw) {
            wx.showToast({
                title: '超出可提现金额',
                icon: 'none',
                duration: 1500
            });
        } else if (!that.data.name) {
            wx.showToast({
                title: '请输入姓名',
                icon: 'none',
                duration: 1500
            });
        } else if (!that.data.bank) {
            wx.showToast({
                title: '请选择开户行',
                icon: 'none',
                duration: 1500
            });
        } else if (!that.data.bankNumber) {
            wx.showToast({
                title: '请输入银行卡号',
                icon: 'none',
                duration: 1500
            });
        } else if (!that.data.phoneNumber) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 1500
            });
        } else if (!that.data.checked) {
            wx.showToast({
                title: '请同意兼职合同',
                icon: 'none',
                duration: 1500
            });
        } else {
          var openid = wx.getStorageSync('openid');
          var withdrawNum = that.data.value;
          var name = that.data.name;
          var bank = that.data.bank;
          var cardNum = that.data.bankNumber;
          var cardPhone = that.data.phoneNumber;
          var salary =that.data.salary;
          var jifen=that.data.jifen;
          var radio1=that.data.radio1;
          var drawmethod=that.data.drawmethod;

          wx.request({
            url: Host + 'Mine/savewithdraw',
            data: { openid, withdrawNum, name, bank, cardNum, cardPhone, salary, jifen, jifen, radio1, drawmethod},
            method: 'post',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              if (res.data.code == 200) {
                wx.showToast({
                  title: '提现成功',
                  icon: 'success',
                  duration: 1500
                });
              } else {
                wx.showToast({
                  title: res.data.errmsg,
                  icon: 'none',
                })
              }
              setTimeout(function () {
                wx.navigateTo({
                  url: '/pages/balance/balance',
                })
              }, 1000)
            },
          })
        }

    },
    openShow:function(){
        this.setData({
            mengShow:true,
        })
    },
    disagree: function() {
        var that = this;
        that.setData({
            mengShow: false,
            checked: false,
        })
    },
    agree: function() {
        var that = this;
        that.setData({
            mengShow: false,
            checked: true,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        function toDecimal2(x) { //强制保留2位小数
            var f = parseFloat(x);
            if (isNaN(f)) {
                return false;
            }
            var f = Math.round(x * 100) / 100;
            var s = f.toString();
            var rs = s.indexOf('.');
            if (rs < 0) {
                rs = s.length;
                s += '.';
            }
            while (s.length <= rs + 2) {
                s += '0';
            }
            return s;
        }
        // console.log(toDecimal2('3.12545'), toDecimal2(3))
        var that = this;
        var allWithdraw = toDecimal2(that.data.allWithdraw);
        this.setData({
            allWithdraw: allWithdraw,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
      var openid = wx.getStorageSync('openid');
      //console.log(openid)
      var that = this;
      wx.request({
        url: Host + 'Mine/mineAssetsDrawback',
        data: { openid },
        success: function (res) {
          if (res.data.code == 200) {
            that.setData({
              allWithdraw: res.data.drawcommission,
              value: res.data.drawcommission,
              salary: res.data.salary,
              name: res.data.bankinfo.name ? res.data.bankinfo.name:"",
              bank: res.data.bankinfo.bank ? res.data.bankinfo.bank : "",
              bankNumber: res.data.bankinfo.number ? res.data.bankinfo.number:"",
              phoneNumber: res.data.bankinfo.phone ? res.data.bankinfo.phone:"",
            });
            WxParse.wxParse('content', 'html', res.data.contract_content, that, 5)
          } else {
            wx.showToast({
              title: res.data.errmsg,
              icon: 'none',
            })
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/personal/personal',
              })
            }, 1000)
          }
        },
      })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})